<?
##########################################################################
#		DOCUMENTATION
##########################################################################
# profile script. Start the session and handle the cookies. When everything is
# set returns to view.php
#
# © LiberLab group, 2011
##########################################################################

require_once("dataStructure/user.php");
require_once("dynamic/base/sendData.php");

session_start();

class Signin extends SendData{
	public function __construct() {
		parent::__construct("signin");
	}
	
	public function main(){
		#nickname, password1, password2, email, name, surname
		$nicknameNew = strtolower(getParam("nickname", ""));
		$passwordOld = getParam("password_old", "");
		$password1 = getParam("password1", "");
		$password2 = getParam("password2", "");
		if (!$password1 or !$password2){
			$password1 = NULL; $password2 = NULL;
		}
		$email = getParam("email", "");
		$authorName = getParam("name", " ");
		$authorSurname = getParam("surname", " ");
		
		$query = $this->queryBuilder(array("nickname", "password1", "password2", "password_old", "email", "name", "surname",
									  "no_user", "no_password", "no_email", "no_match", "generic", "nickname_over", "no_login"));

		if ($nicknameNew and $passwordOld and $email and $this->mailCheck($email) 
			and (($password1 and $password1 == $password2) or !$password1)){
			$userClass = new User();
			# login before trying
			$passwordOldEncr = $userClass->login($this->getSession("nickname"), $passwordOld);
			$newUserId = 0; # default, prevent errors when you don't change the nickname
			if ($nicknameNew != $this->getSession("nickname")){
				$newUserId = $userClass->getUserId($nicknameNew)->id;
			}
			if ($passwordOldEncr and !$newUserId){
				$editResult = $userClass->userEdit($this->getSession("nickname"), $passwordOld, $password1, $nicknameNew, $email,
												   NULL, $authorName, $authorSurname);
				if ($editResult){ # if true, save the changes in SESSION vars
					if ($nicknameNew){
						$this->setSession("nickname", $nicknameNew);
						# updating the cookies only if the user wants them!
						if ($this->getCookie("nickname")){
							$this->setCookie("nickname", $nicknameNew);
						}
					}
					if ($password1){
						$passwordEncrypted = $userClass->encryptPassword($password1);
						$this->setSession("password", $passwordEncrypted);
						if ($this->getCookie("password")){
							$this->setCookie("password", $passwordEncrypted);
						}
					}
					return $this->redirect($query);
				}else{
					$this->checkInsert(1, "&generic=1"); 
				}
			}else{
				$this->checkInsert(($newUserId), "&nickname_over=1");
				$this->checkInsert(1, "&no_login=1");
			}
		}else{
			$this->checkInsert((!$nicknameNew), "&no_user=1");
			$this->checkInsert((!$passwordOld) ,"&no_password=1");
			$this->checkInsert((!($email and $this->mailCheck($email))), "&no_email=1");
			$this->checkInsert(($password1 and ($password1 != $password2)), "&no_match=1");
		}
		return $this->redirect($this->queryFinalize($checkArray, $query)); 
	}

	public function __destruct() {
		parent::__destruct();
	}
};

$signin = new Signin();
$signin->main();

?>
